<?php
/**
 * Created by PhpStorm.
 * User: dungnt13
 * Date: 11/19/14
 * Time: 4:32 PM
 */

namespace Admin;

use AdminController,
    Jinput,
    Input,
    Validator,
    Config,
    View,
    Redirect,
    Topic,
    Sentry,
    Str;


class TopicsController extends AdminController
{
    public function getIndex()
    {
        $data['appends']['withTrashed'] = Jinput::get('withTrashed', false);
        $data['appends']['onlyTrashed'] = Jinput::get('onlyTrashed', false);
        $data['appends']['status'] = Jinput::get('status', 'on');
        $myModel = new \MyModel();
        $arrParams = array();
        $arrParams['paginate']['appends'] = $data['appends'];
        $arrParams['conditions'][] = array('field' => 'status', 'opr' => '=', 'value' => $data['appends']['status']);
        $data['topics'] = $myModel->listItems('Topic', $arrParams);
        return View::make('backend/topics/index', compact('data'));
    }

    public function getCreate()
    {
        //        if (!Sentry::getUser()->hasAnyAccess(['topic', 'topic.create']))
//            return View::make('backend/notallow');
        $data['topic'] = new Topic();
        return View::make('backend/topics/create', compact('data'));
    }

    public function getEdit($id)
    {
//        if(!Sentry::getUser()->hasAnyAccess(['topic','topic.edit']))
//            return View::make('backend/notallow');
        if (is_null($data['topic'] = Topic::find($id)))
            return Redirect::route('topics/admin');
        return View::make('backend/topics/create', compact('data'));
    }

    public function getDelete()
    {

    }

    public function getRestore()
    {

    }

    public function postCreate()
    {
        $data = Input::get('data');
        $rules = array('title' => 'required|min:4');
        $validation = Validator::make($data['Topic'], $rules);
        if ($validation->fails())
            return Redirect::back()->withInput()->withErrors($validation);
        $myModel = new \MyModel();
        $id = $myModel->saveItem('Topic', $data['Topic']);
        return Redirect::route('update/topics/admin', $id)->with('success', 'Chủ đề đã được lưu thành công');

    }
} 